package com.zshield.run;

import org.apache.commons.cli.*;

public class OptionsProcessor {
	public static CommandLine parseArg(String[] args) throws ParseException {
        Options options = new Options();
        options.addOption("b","bootstrap_server", true, "kafka server");
		options.addOption("n","es_number_of_replica", true, "es number of replica");
		options.addOption("e","es_host", true, "es host");
		options.addOption("u","upgrade", false, "upgrade");
		options.addOption("h","help", false, "usage help");

		CommandLineParser parser = new PosixParser();
		CommandLine cmdLine = parser.parse(options, args);
		if (cmdLine.hasOption("help") || cmdLine.hasOption("h")) {
			usage();
			System.exit(-1);
		}
		if (!cmdLine.hasOption("bootstrap_server")) {
			throw new ParseException("bootstrap_server is indispensable");
		}
		if (!cmdLine.hasOption("es_number_of_replica")) {
			throw new ParseException("es_number_of_replica is indispensable");
		}
		if (!cmdLine.hasOption("es_host")) {
			throw new ParseException("es_host is indispensable");
		}

		return cmdLine;
	}

	private static void usage() {
		StringBuilder helpInfo = new StringBuilder();
		helpInfo.append("-b").append("\t\t\t --bootstrap_server  kafka server,for example: -bootstrap_server 192.168.10.226:9092").append("\n")
				.append("-n").append("\t\t\t --es_number_of_replica  es number of replica,for example: -es_number_of_replica 2").append("\n")
				.append("-e").append("\t\t\t --es_host  es host,for example: -es_host 192.168.10.226").append("\n")
				.append("-u").append("\t\t\t --upgrade  upgrade,for example: -upgrade true").append("\n")
				.append("-h").append("\t\t\t --help  help command").append("\n");

		System.out.println(helpInfo.toString());
	}
}
